﻿using System;
using System.Net;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity;
using System.Data.Entity.Database;
using MSBTC.Model;
using MSBTC.WinFormHelpers;
using MSBTC.WinForms;

namespace MSBTC
{
    public partial class Login : Form
    {      
        //Panel panel1 = new Panel();
        PMSContext dbcontext = new PMSContext();
        MDIParent mdipar = new MDIParent();
        Session credentials = new Session();
        SessionHelper seshelper = new SessionHelper();
        FormHelpers helper = new FormHelpers();

        public Login()
        {
            InitializeComponent();
            this.txtPassword.PasswordChar = '\u25CF';     
        }

        private void Login_Load(object sender, EventArgs e)
        {
            //helper.ResetConfig();
            //if (File.Exists(helper.ConfigXML) == true)
            //{
            //    if (helper.Read_Configuration() == true)
            //    {
            //        txtUsername.Clear();
            //        txtPassword.Clear();
            //    }
            //    else
            //    {
            //        MessageBox.Show(helper.ex, "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //        Application.Exit();
            //    }
            //}
            //else
            //{
            //    MessageBox.Show(helper.ex, "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    Application.Exit();
            //}
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string date = DateTime.Today.ToShortDateString();
            if ((txtUsername.Text == "") || (txtPassword.Text == ""))
            {
                MessageBox.Show("Username or Password is empty", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                try
                {
                    if (helper.CheckConfig() == true)
                    {
                        //helper.Read_Configuration();
                        var log = (from u in dbcontext.Users
                                   where u.Username == txtUsername.Text && u.Password == txtPassword.Text
                                   && u.Status == "Active"
                                   select u).SingleOrDefault();

                        if (log == null)
                        {
                            MessageBox.Show("Invalid Username or password, or your account has been deactivated by administrator", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        else
                        {
                            string ip = seshelper.Session_GetIP();
                            var ses = (from sess in dbcontext.Sessions
                                       where sess.Date_Logout == null && sess.IPAddress == ip
                                       && sess.Userid_FK == log.Userid_PK
                                       select sess).SingleOrDefault();

                            if (ses == null)
                            {
                                credentials.Session_Name = log.Name;
                                credentials.Role_Name = log.Role_Name;
                                credentials.Date_Created = date;
                                credentials.Userid_FK = log.Userid_PK;
                                credentials.IPAddress = ip;

                                dbcontext.Entry(credentials).State = EntityState.Added;
                                dbcontext.SaveChanges();

                            }
                            helper.usrname(credentials.Session_Name);
                            lblSuccess.Text = "Successful";

                            this.Hide();
                            mdipar.Show();
                            mdipar.Enabled = true;
                            mdipar.closeform();
                            mdipar.MDIContainer("Main Form");

                        }
                    }
                    else
                    {
                        MessageBox.Show(helper.ex, "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
                catch (Exception exc)
                {
                    MessageBox.Show(exc.Message.ToString() + ", Please verify the connection or IP has been reset.", "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    helper.error = "ERROR";
                    //DBConnForm dbform = new DBConnForm();
                    //this.Hide();
                    //dbform.Show();
                }
            }
           
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            //helper.DBSql.Close();
            //helper.DBSql.Dispose();
            Application.Exit();
        }
   
    }
  
}
